<?php defined('SYSPATH') or die('No direct script access.');

/*
 * 问答功能
 */

class Controller_Pc_Api_Standard_Question extends Controller_Pc_Api_Base
{

    public function before()
    {
        parent::before();
    }

    //获取产品问答数
    public function action_questioncount()
    {
        $typeid = Common::remove_xss($this->request_body->content->typeid);//模型id
        $articleid = Common::remove_xss($this->request_body->content->articleid);//文章或者产品id
        $number = Model_Question::get_question_num($typeid, $articleid);
        if ($number) {
            $this->send_datagrams($this->client_info['id'], ['number' => $number], $this->client_info['secret_key']);
        } else {
            $this->send_datagrams($this->client_info['id'], ['number' => 0], $this->client_info['secret_key']);
        }
    }

    //获取问答数据
    public function action_questionlist()
    {
        $typeid = Common::remove_xss($this->request_body->content->typeid);//模型id
        $articleid = Common::remove_xss($this->request_body->content->articleid);//文章或者产品id
        $questype = Common::remove_xss($this->request_body->content->questype);//问题类型，0为产品相关问答，1为一般问答
        $currentpage = Common::remove_xss($this->request_body->content->currentpage);//当前页面
        $pagesize = Common::remove_xss($this->request_body->content->pagesize);//每页数量
        $mid = Common::remove_xss($this->request_body->content->mid);//会员id（需要查询会员问答使用）
        $currentpage = empty($currentpage) ? 1 : $currentpage;
        $pagesize = empty($pagesize) ? 1 : $pagesize;
        if (empty($typeid)) {
            $this->send_datagrams($this->client_info['id'], [], $this->client_info['secret_key'], false, '未找到问答数据');
        } else {
            if (empty($mid)) {
                $list = Model_Question::search_result($currentpage, $pagesize, $questype, $typeid, $articleid);
            } else {
                $list = Model_Question::question_list($mid, $questype, $currentpage, $pagesize);
            }
            $this->send_datagrams($this->client_info['id'], $list, $this->client_info['secret_key']);
        }
    }

    //提问功能获得会员信息
    public function action_addquestion()
    {
        $mid = Common::remove_xss($this->request_body->content->mid);//会员id
        if (!empty($mid)) {
            $userInfo = Model_Member::get_member_byid($mid);
            $this->send_datagrams($this->client_info['id'], $userInfo, $this->client_info['secret_key']);
        } else {
            $this->send_datagrams($this->client_info['id'], [], $this->client_info['secret_key'], false, '未找到会员信息');
        }
    }

    //提问功能提交
    public function action_questionsub()
    {
        $typeid = Common::remove_xss($this->request_body->content->typeid);//模型id
        $articleid = Common::remove_xss($this->request_body->content->articleid);//文章或者产品id
        $mobile = Common::remove_xss($this->request_body->content->mobile);//手机号
        $is_anonymous = intval($this->request_body->content->is_anonymous);//是否匿名
        $content = Common::remove_xss($this->request_body->content->content);//内容
        $mid = Common::remove_xss($this->request_body->content->mid);//会员id

        $arr = array();
        $arr['typeid'] = $typeid;
        $arr['productid'] = $articleid;
        $arr['content'] = $content;
        $arr['phone'] = $mobile;
        $arr['addtime'] = time();
        //是否匿名
        if (!$is_anonymous) {
            $member = Model_Member::get_member_byid($mid);
            $arr['memberid'] = $member['mid'];
            $arr['nickname'] = $member['nickname'];
        } else {
            $arr['memberid'] = 0;
            $arr['nickname'] = '匿名';
        }
        $flag = DB::insert('question', array_keys($arr))->values(array_values($arr))->execute();
        if ($flag) {
            $model_info = Model_Model::get_module_info($typeid);
            $jifen = Model_Jifen::reward_jifen('sys_write_' . $model_info['pinyin'], $arr['memberid']);
            if (!empty($jifen)) {
                St_Product::add_jifen_log($arr['memberid'], '发布' . $model_info['modulename'] . '送积分' . $jifen, $jifen, 2);
            }
        }
        $this->send_datagrams($this->client_info['id'], $flag, $this->client_info['secret_key']);
    }

}